﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace WinFormsApp1
{
    public partial class MouseTrackerForm : Form
    {
        public MouseTrackerForm()
        {
            InitializeComponent();
            this.Text = "鼠标轨迹绘制";
            this.Size = new Size(300, 200);

            btnStartTracking = new Button();
            btnStartTracking.Text = "开始绘制鼠标轨迹";
            btnStartTracking.Size = new Size(150, 50);
            btnStartTracking.Location = new Point(75, 30);
            btnStartTracking.Click += BtnStartTracking_Click;

            btnStopTracking = new Button();
            btnStopTracking.Text = "停止绘制";
            btnStopTracking.Size = new Size(150, 50);
            btnStopTracking.Location = new Point(75, 90);
            btnStopTracking.Click += BtnStopTracking_Click;
            btnStopTracking.Enabled = false;

            this.Controls.Add(btnStartTracking);
            this.Controls.Add(btnStopTracking);
        }

        private void MouseTrackerForm1_Load(object sender, EventArgs e)
        {

        }
        private Button btnStartTracking;
        private Button btnStopTracking;
        private MouseTracker tracker;

   

        private void BtnStartTracking_Click(object sender, EventArgs e)
        {
            tracker = new MouseTracker();
            tracker.Show();
            btnStartTracking.Enabled = false;
            btnStopTracking.Enabled = true;
        }

        private void BtnStopTracking_Click(object sender, EventArgs e)
        {
            if (tracker != null)
            {
                tracker.Close();
                tracker = null;
            }
            btnStartTracking.Enabled = true;
            btnStopTracking.Enabled = false;
        }
    }

    public class MouseTracker : Form
    {
        [DllImport("user32.dll")]
        static extern bool GetCursorPos(out POINT lpPoint);

        [StructLayout(LayoutKind.Sequential)]
        public struct POINT
        {
            public int X;
            public int Y;
        }

        [DllImport("user32.dll", SetLastError = true)]
        static extern int GetWindowLong(IntPtr hWnd, int nIndex);

        [DllImport("user32.dll")]
        static extern int SetWindowLong(IntPtr hWnd, int nIndex, int dwNewLong);

        private System.Windows.Forms.Timer timer;
        private Point[] points = new Point[100];
        private int index = 0;

        public MouseTracker()
        {
            this.FormBorderStyle = FormBorderStyle.None;
            this.WindowState = FormWindowState.Maximized;
            this.TopMost = true;
            this.BackColor = Color.Black;
            this.Opacity = 0.5;
            this.ShowInTaskbar = false;

            // 设置窗口为透明点击穿透
            int initialStyle = GetWindowLong(this.Handle, -20);
            SetWindowLong(this.Handle, -20, initialStyle | 0x80000 | 0x20);

            timer = new System.Windows.Forms.Timer();
            timer.Interval = 10;
            timer.Tick += UpdateMousePosition;
            timer.Start();
        }

        private void UpdateMousePosition(object sender, EventArgs e)
        {
            POINT p;
            if (GetCursorPos(out p))
            {
                points[index] = new Point(p.X, p.Y);
                index = (index + 1) % points.Length;
                this.Invalidate();
            }
        }

        protected override void OnPaint(PaintEventArgs e)
        {
            base.OnPaint(e);

            using (Pen pen = new Pen(Color.Cyan, 3))
            {
                for (int i = 0; i < points.Length - 1; i++)
                {
                    int current = (index + i) % points.Length;
                    int next = (index + i + 1) % points.Length;

                    if (points[current] != Point.Empty && points[next] != Point.Empty)
                    {
                        e.Graphics.DrawLine(pen, points[current], points[next]);
                    }
                }
            }
        }
    }

}
